// router
import router, { routes } from './router'

// store
import store from './store'

// nprogress
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// NProgress configure
NProgress.configure({ showSpinner: true })

// async 修饰，因为里面会使用到 await
router.beforeEach(async (to, from, next) => {
  // to and from are Route Object,next() must be called to resolve the hook
  // start process bar
  NProgress.start()
  document.title = to.meta.title

  const hasRoutes = store.getters.routes.length > 0
  console.log(hasRoutes)
  if (!hasRoutes) {
    await store.dispatch('permission/setRoutes', routes)
  }
  console.log(store.getters.routes)
  // 必须释放此钩子
  next()
})

router.afterEach(route => {
  // these hooks do not get a next function and cannot affect the navigation
  NProgress.done()
})
